
#include "wm_internal_flash.h"
#include "lcd.h"
#include "spiflash.h"

typedef struct
{
    uint32_t   magic_num;       //  4 byte   目前固定为0x12345678
    char  version[10];     // 10 byte
    uint16_t   image_count;     //  2 byte
}ImageHeader;

typedef struct 
{
    uint32_t  strAddr;          //  4 byte
    uint32_t  size;             //  4 byte
    char image_name[16];    // 16 byte
}ImageInfo;                // image_count



// typedef struct 
// {
//     uint16_t width;
//     uint16_t height;
// }ImageSize;

ImageHeader image_header;

static int get_image_header()
{
    int ret = 0;
    int len = 0;
    len = sizeof(ImageHeader);
    ret = HAL_FLASH_Read(0x96000, (uint8_t *)&image_header, len);
	if(ret != 0)
	{
		printf("---> get_image_header fail !\n");
		return ret;
	}
    // 防止打印%s越界
    image_header.version[9] = '\0';
#if (0) // test
    uint8_t *buf = (uint8_t *)&image_header;
    printf("---> len: %d\n", len);
    for(uint16_t i = 0; i < len; i++)
    {
        printf("%02X ", buf[i]);
    }
    printf("\n");
#endif
    // if(image_header.magic_num != 0x12345678)
    // {
	// 	printf("---> get_image_header fail !\n");
	// 	return ret;
    // }
    printf("---> magic_num:   %08X\n", image_header.magic_num);
    printf("---> version:     %s\n",   image_header.version);
    printf("---> image_count: %d\n\n",   image_header.image_count);
    return image_header.image_count;
}

int get_image_list_show(void)
{
    int ret = 0;
    uint16_t image_count = 0;
    uint16_t image_buf_len = 0;
    uint16_t offset = 0;

    uint16_t index = 0;

    uint8_t *image_info = NULL;

    ImageInfo *image_list = NULL;
    // ImageSize  image_size = {0};
    uint8_t image_size[4] = {0};
 
    image_count = get_image_header();
    image_buf_len = image_count * sizeof(ImageInfo);
    offset = sizeof(ImageHeader);

    image_info = malloc(image_buf_len);
    if(image_info == NULL)
    {
        printf("[%s] mem err\n", __func__);
        return -1;
    }
    ret = HAL_FLASH_Read(offset, image_info, image_buf_len);
	if(ret != 0)
	{
		printf("---> get_image_list fail %d !\n", ret);
		free(image_info);
		return ret;
	}
    image_list = (ImageInfo *)image_info;
    printf("---> strAddr  imgSize  imgName\n");
    for(uint16_t i = 0; i < image_count; i++)
    {
        image_list[i].image_name[15] = '\0';
        printf("---> %08X %08X %s\n", image_list[i].strAddr, image_list[i].size, image_list[i].image_name);
    }

	if(index >= image_count)
	{
		index = 0;
	}
	ret = HAL_FLASH_Read(image_list[index].strAddr, (uint8_t *)&image_size, sizeof(image_size));
	if(ret != 0)
	{
		printf("---> image_size fail %d !\n", ret);
		free(image_list);
		return ret;
	}
	uint8_t *p = (uint8_t *)&image_size;
	// printf("%02X %02X %02X %02X\n", p[0],p[1],p[2],p[3]);
	uint16_t width =  (p[0] << 8) + p[1];
	uint16_t height =  (p[2] << 8) + p[3];
	printf("---> width: %d height %d\n", width, height);
	lcd_clear(BLACK);
	lcd_full_picture_from_spiflash(width, height, image_list[index].strAddr + 4);
	index++;
	HAL_Delay(3000);
	
    free(image_list);
    return 0;
}
